package com.foin.city.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.foin.city.R;
import com.foin.city.bean.City;
import com.foin.city.itemclick.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 城市列表适配器
 */
public class CityAdapter extends RecyclerView.Adapter<CityAdapter.CityViewHolder> {

    // RecyclerView 的第一个item，肯定是展示StickyLayout的.
    public static final int FIRST_STICKY_VIEW = 1;
    // RecyclerView 除了第一个item以外，要展示StickyLayout的.
    public static final int HAS_STICKY_VIEW = 2;
    // RecyclerView 的不展示StickyLayout的item.
    public static final int NONE_STICKY_VIEW = 3;

    private Context mContext;
    private List<City> mCityList;
    private OnItemClickListener onItemClickListener;

    public CityAdapter(Context context, List<City> list) {
        mContext = context;
        mCityList = list;
    }

    @NonNull
    @Override
    public CityViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(mContext).inflate(R.layout.item_view_city, parent, false);
        return new CityViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(@NonNull CityViewHolder holder, final int position) {
        if (position == 0) {
            holder.mLetterTv.setVisibility(View.VISIBLE);
            holder.mLetterTv.setTextColor(mContext.getResources().getColor(R.color.textColor999));
            holder.mLetterTv.setText(mCityList.get(position).getLetter());
            // 第一个item的吸顶信息肯定是展示的，并且标记tag为FIRST_STICKY_VIEW
            holder.itemView.setTag(FIRST_STICKY_VIEW);
        } else {
            holder.mLetterTv.setTextColor(mContext.getResources().getColor(R.color.textColorMain));
            if (!TextUtils.equals(mCityList.get(position).getLetter(), mCityList.get(position - 1).getLetter())) {
                holder.mLetterTv.setVisibility(View.VISIBLE);
                holder.mLetterTv.setText(mCityList.get(position).getLetter());
                holder.itemView.setTag(HAS_STICKY_VIEW);
            } else {
                holder.mLetterTv.setVisibility(View.GONE);
                holder.itemView.setTag(NONE_STICKY_VIEW);
            }
        }
        holder.itemView.setContentDescription(mCityList.get(position).getLetter());
        holder.mCityNameTv.setText(mCityList.get(position).getName());
        holder.mCityNameTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position, view);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mCityList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public class CityViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.letter)
        TextView mLetterTv;
        @BindView(R.id.city_name)
        TextView mCityNameTv;

        public CityViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
